package com.huibo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huibo.common.R;
import com.huibo.common.WorkForm;
import com.huibo.entity.Company;
import com.huibo.entity.Employee;
import com.huibo.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 员工管理
 */
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 测试
     * @return
     */
    @GetMapping("/test")
    public String test(){
        Employee byId = employeeService.getById(1);
        System.out.println(byId.toString());
        return byId.toString();
    }

    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){

        //1.将前端发来的密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.根据发来的用户名去查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getName,employee.getName());
        Employee emp = employeeService.getOne(queryWrapper);

        //3.如果没有查询到则返回登录失败结果
        if(emp == null){
            return R.error("登录失败");
        }

        //4.密码的比对，如果不一致就返回失败
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }

        //5.查看员工状态是否是正常可用的
        if(emp.getStatus()== 0){
            return R.error("账号禁用");
        }

        //6.登录成功，将员工id存入Session并返回登录成功结果,(employee)是当前登录用户的key值)
        //如果是管理员，那么就将Session中存入manager
        if(emp.getName().equals("admin")){
            request.getSession().setAttribute("manager",emp.getId());
        }else{
            request.getSession().setAttribute("employee",emp.getId());
        }
        return R.success(emp);
    }



    /**
     * 员工退出
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        //清理session
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee){
//        log.info("新增员工，员工信息：{}",employee.toString());
        String name = employee.getName();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(name),Employee::getName,employee.getName());
        Employee one = employeeService.getOne(queryWrapper);
        if(one != null){
            return R.error("用户名已存在！");
        }
        //设置初始密码，进行MD5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employeeService.save(employee);

        return R.success("新增员工成功");
    }

    /**
     * 修改员工
     * @param employee
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        boolean flag = employeeService.updateById(employee);
        if(flag){
            return R.success("更新成功");
        }else{
            return R.error("更新失败");
        }
    }


    /**
     * 删除员工
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids){
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getId,ids);
        boolean remove = employeeService.remove(queryWrapper);
        if(remove){
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }
    }


    /**
     * 查询所有员工
     * @return
     */
    @PostMapping("/getAll")
    public R<List<Employee>> getAll(@RequestBody Company company){
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getMarketId,company.getName());
        List<Employee> list = employeeService.list(queryWrapper);
        if(list == null){
            return R.error("没有员工！");
        }else{
            return R.success(list);
        }
    }
}
